package com.ruoyi.project.abucoder.util;

import com.ruoyi.project.abucoder.point.domain.UserPoint;
import com.ruoyi.project.abucoder.prize.domain.Prize;

import java.util.List;
import java.util.Random;

public class Lottery {
    public static void main(String[] args) {
        //cj(10,180);
        // 示例：抽奖
        String[] prizes = {"祥瑞·奔雷动", "祥瑞·寒霜凛", "祥瑞、夜摧梦", "祥瑞·眠鲤宿月亭",
                "天赏石", "鲸羽千里", "云山千里", "配饰·虎虎生风", "凤凰·鹤染",
                "挂饰·心上月", "奔虹赤", "鞍具·苍翼", "鞍具·战鹰", "鞍具·凛羽",
                "安梦床·潮生", "发型:霜寒", "发型:折红扇", "发型:天琅", "发型:曦曲",
                "云淡·共潮生", "追风翼·御剑", "义承千斤", "发型:天琅", "月·玳筵",
                "芰荷烟雨", "风筝", "螺带染", "轻容染","狗粮"};
        int  cj=180;
        int  bd=180;
        for (int i = 0; i <cj ; i++) {

//            String winner = weightedRandomSelection(prizes, getWeight(bd));
//            if(winner.contains("天赏") || winner.contains("祥瑞")){
//                bd=180;
//            }else {
//                bd-=1;
//            }
//            System.out.println("恭喜你抽中"+winner+"！再抽"+bd+"次，必出金色道具！");
        }
    }

    public static void cj(int num,int bd){
         double probability = 0.004; // 金
         double probability_xr = 0.1; // 祥瑞
         double probability_ts = 0.9; // 天赏
         double probability_zs = 0.04; // 紫色
         double probability_hs = 0.956; // 红色
        Random random = new Random();
        // 抽奖
        for (int i = 0; i < num; i++) {
            if(bd==180){
                 probability = 0.001; // 金
                 probability_xr = 0.1; // 祥瑞
                 probability_ts = 0.9; // 天赏
                 probability_zs = 0.04; // 紫色
                 probability_hs = 0.959; // 红色
            }
            if(bd==100){
                probability_hs=probability_hs-0.006;
                probability = probability+0.006;
            }
            if(bd==30){
                probability_hs=probability_hs-0.1;
                probability = probability+0.1;
            }
            if(bd==10){
                probability_hs=probability_hs-0.7;
                probability = probability+0.7;
            }
            if(bd==1){
                probability_hs=0;
                probability_zs=0;
                probability = 1;
            }
            double rand = random.nextDouble();
            boolean hs = rand <= probability_hs; // 根据概率判断中奖类型
            boolean zs = probability_hs<rand && rand <= probability_zs+probability_hs; // 根据概率判断中奖类型
            boolean js = probability_zs+probability_hs<rand && rand <= 1; // 根据概率判断中奖类型
            if(hs){
                //System.out.println("红色！"+i);
                bd-=1;
            }else if (zs) {
                //System.out.println("紫色！"+i);
                bd-=1;
            }else if (js) {
                double rand_xr = random.nextDouble();
                boolean xr = probability_ts<rand_xr && rand_xr <= 1; // 根据概率判断中奖类型
                if(xr){
                    System.out.println("恭喜你，中祥瑞了！"+i);
                    bd=180;
                }else {
                    System.out.println("恭喜你，中天赏了！"+i);
                    bd=180;
                }
            }
            //System.out.println("还差"+bd+"必出金色");
        }
    }

    public static Prize weightedRandomSelection(List<Prize> list, double[] weights) {
        if (list.size() != weights.length) {
            throw new IllegalArgumentException("Items and weights arrays must be of the same length.");
        }
        double sum = 0.0;
        for (double weight : weights) {
            sum += weight;
        }
        double random = new Random().nextDouble() * sum;
        for (int i = 0; i < list.size(); i++) {
            random -= weights[i];
            if (random <= 0) {
                return list.get(i);
            }
        }
        // 应该不会执行到这里，除非有负权重或者计算错误
        return list.get(list.size() - 1);
    }
    public static double[] getWeight(int bd){
        double[] weights = new double[0];
        if(bd>=100){
            weights = new double[] {0.0001, 0.0001, 0.0001, 0.0001,0.0036,
                    0.002,0.002,0.002,0.002,0.002,0.002,0.002,0.002,
                    0.002,0.002,0.002,0.002,0.002,0.002,0.002,0.002,
                    0.002,0.002,0.002,0.002,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195}; // 概率总和应为1

        }
        if(bd<100){
            weights = new double[] {0.0001, 0.0001, 0.0001, 0.0001,0.0036,
                    0.002,0.002,0.002,0.002,0.002,0.002,0.002,0.002,
                    0.002,0.002,0.002,0.002,0.002,0.002,0.002,0.002,
                    0.002,0.002,0.002,0.002,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195};
        }
        if(bd<30){
            weights = new double[] {0.0001, 0.0001, 0.0001, 0.0001,0.0036,
                    0.002,0.002,0.002,0.002,0.002,0.002,0.002,0.002,
                    0.002,0.002,0.002,0.002,0.002,0.002,0.002,0.002,
                    0.002,0.002,0.002,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195,0.1195};
        }
        if(bd<10){
            weights = new double[] {0.046, 0.046, 0.046, 0.046,0.736,
                    0.002,0.002,0.002,0.002,0.002,0.002,0.002,0.002,
                    0.002,0.002,0.002,0.002,0.002,0.002,0.002,0.002,
                    0.002,0.002,0.002,0.002,0.005,0.005,0.005,0.005,0.005,0.005,0.005,0.005};
        }
        if(bd==1){
            weights = new double[] {0.05, 0.05, 0.05, 0.05,0.8,
                    0,0,0,0,0,0,0,0,
                    0,0,0,0,0,0,0,0,
                    0,0,0,0,0,0,0,0,0,0,0,0,};
        }
        return weights;
    }
}